import Vue from 'vue'

import Cookies from 'js-cookie'

import 'normalize.css/normalize.css' // a modern alternative to CSS resets

import Element from 'element-ui'
import './styles/element-variables.scss'

import '@/styles/index.scss' // global css
import { vuePlugin } from '@/components/BpmnDesigner/package/highlight'
import 'highlight.js/styles/atom-one-dark-reasonable.css'
Vue.use(vuePlugin)
import MyPD from '@/components/BpmnDesigner/package/index.js'
Vue.use(MyPD)
import '@/components/BpmnDesigner/package/theme/index.scss'
import 'bpmn-js/dist/assets/diagram-js.css'
import 'bpmn-js/dist/assets/bpmn-font/css/bpmn.css'
import 'bpmn-js/dist/assets/bpmn-font/css/bpmn-codes.css'

import App from './App'
import store from './store'
import router from './router'

import './icons' // icon
import './permission' // permission control
import './utils/error-log' // error log

import * as filters from './filters' // global filters

import permission from './directive/permission' // usse v-permission
import role from './directive/role' // usse v-role
import enter2tab from './directive/enter2tab' // usse enter2tab
/**
 * If you don't want to use mock-server
 * you want to use MockJs for mock api
 * you can execute: mockXHR()
 *
 * Currently MockJs will be used in the production environment,
 * please remove it before going online! ! !

import { mockXHR } from '../mock'
if (process.env.NODE_ENV === 'production') {
  mockXHR()
}
 */

Vue.use(Element, {
  size: Cookies.get('size') || 'medium' // set element-ui default size
})

Vue.use(permission)
Vue.use(role)
Vue.use(enter2tab)

// register global utility filters
Object.keys(filters).forEach(key => {
  Vue.filter(key, filters[key])
})

// 定义一个全局funciton,用于查询时，将非空字段传到后台
const copyNotBlandChild = (from) => {
  if (from instanceof Date) {
    return from
  }
  let to = null
  Object.keys(from).forEach(key => {
    if (from[key]) {
      if (typeof from[key] === 'string') {
        if (from[key].trim() !== '') {
          if (to === null) {
            to = {}
          }
          to[key] = from[key].trim()
        }
      } else if (typeof from[key] === 'object') {
        const child = copyNotBlandChild(from[key])
        if (child !== null) {
          if (to === null) {
            to = {}
          }
          to[key] = child
        }
      } else {
        if (from[key] !== null) {
          if (to === null) {
            to = from.constructor === Array ? [] : {}
          }
          to[key] = from[key]
        }
      }
    }
  })
  return to
}
Vue.prototype.copyNotBlankField = function(from, to) {
  Object.keys(from).forEach(key => {
    if (typeof from[key] === 'number' || typeof from[key] === 'boolean') {
      to[key] = from[key]
    } else {
      if (from[key]) {
        if (typeof from[key] === 'string') {
          if (from[key].trim() !== '') {
            to[key] = from[key].trim()
          }
        } else if (typeof from[key] === 'object') {
          const obj = copyNotBlandChild(from[key])
          if (obj !== null) {
            to[key] = obj
          }
        } else {
          to[key] = from[key]
        }
      }
    }
  })
}

new Vue({
  el: '#app',
  router,
  store,
  render: h => h(App)
})
